package com.bml.architect.rabbitmq.pubsub;

import com.bml.architect.rabbitmq.utils.RabbitConstant;
import com.bml.architect.rabbitmq.utils.RabbitUtils;
import com.rabbitmq.client.*;

import java.io.IOException;

public class User1Consumer {

    public static void main(String[] args) throws Exception {

        Connection connection = RabbitUtils.getConnection();
        final Channel channel = connection.createChannel();
        channel.queueDeclare(RabbitConstant.attention_user1_queue,false,false,false,null);
        //queueBind用于将队列与交换机绑定
        //参数1：队列名 参数2：交互机名  参数三：路由key（暂时用不到)
        channel.queueBind(RabbitConstant.attention_user1_queue,RabbitConstant.exchange_attention,"");
        channel.basicQos(1);
        channel.basicConsume(RabbitConstant.attention_user1_queue,false,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("user1 关注的用户发表了新的文章，标题为：" + new String(body));
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        });


    }
}
